﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using devtm.Configuration;
using devtm.Collections;
using System.ComponentModel;
using System.Reflection;

namespace devtm.Editor.Configuration.Command
{

    public enum TypeChange
    {
        Add,
        Remove,
        Change
    }


    public class Command : devtm.Editor.Configuration.Command.ICommand
    {
        //private IElementSection section;
        //private TypeChange typeChange;
        private object instance;
        private System.Reflection.PropertyInfo property;


        #region ctor


        public Command(IElementSection section, TypeChange typeChange, object instance, System.Reflection.PropertyInfo property, object oldValue, object newValue)
        {
            // TODO: Complete member initialization
            Element = section;
            Section = section.Section;
            Name = Section.SectionInformation.Name;
            Change = typeChange;

            this.instance = instance;
            this.property = property;

            ValueUndo = oldValue;
            this.ValueRedo = newValue;

        }

        #endregion


        #region Properties

        public IElementSection Element { get; private set; }
        public ConfigurationSection Section { get; private set; }
        public TypeChange Change { get; private set; }
        public object ValueUndo { get; private set; }
        public object ValueRedo { get; private set; }
        public string Name { get; private set; }
        public bool IsUndo { get; private set; }
        public bool IsRedo { get; set; }

        #endregion

        #region Actions


        public void Undo()
        {



            if (UCConfigEdit.SelectedItem == Element)
            {
                foreach (IConfigProperty prop in UCConfigEdit.PropertiesListe)
                {
                    if (property.Name == prop.PropertyName)
                    {
                        prop.Value = ValueUndo;
                        return;
                    }
                }

            }

            property.SetValue(instance, ValueUndo, null);

        }

        public void Redo()
        {

            if (UCConfigEdit.SelectedItem == Element)
            {
                foreach (IConfigProperty prop in UCConfigEdit.PropertiesListe)
                {
                    if (property.Name == prop.PropertyName)
                    {
                        prop.Value = ValueRedo;
                        return;
                    }
                }

            }

            property.SetValue(instance, ValueRedo, null);

        }


        #endregion

    }


}
